//
//  JOTabBarViewController.m
//  新浪微博模拟
//
//  Created by song on 15/3/24.
//  Copyright (c) 2015年 song. All rights reserved.
//

#import "JOTabBarViewController.h"
#import "JOHomeViewController.h"
#import "JOMessageViewController.h"
#import "JODiscoverController.h"
#import "JOMeController.h"
#import "JOTarBar.h"
#import "JONavigationController.h"
#import "AddWeiboController.h"


@interface JOTabBarViewController () <JOTabBarDelegate>

@property(nonatomic, weak) JOTarBar *customTabBar;

@end

@implementation JOTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupTabBar];
    [self setupAllTabBarController];
    


  
}





-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    NSLog(@"%@", self.tabBar.subviews);
    for(UIView *child in self.tabBar.subviews)
    {
        if([child isKindOfClass:[UIControl class]])
        {
            [child removeFromSuperview];
        }
    }
}

-(void) pulsButtonClick
{
    AddWeiboController *add = [[UIStoryboard storyboardWithName:@"NewWeibo" bundle:nil] instantiateViewControllerWithIdentifier:@"addweibo"];
    
    [self presentViewController:add animated:YES completion:nil];
    
}

-(void)setupTabBar
{
    JOTarBar *bar = [[JOTarBar alloc] init];
//    bar.backgroundColor = [UIColor redColor];
    bar.frame = self.tabBar.bounds;
    bar.delegate = self;
    self.customTabBar = bar;
    [self.tabBar addSubview:bar];
}


-(void)tabBar:(JOTarBar *)tabBar didSelectButtonFrom:(int)from to:(int)to
{
    NSLog(@"-----%d  -----------%d" , from , to);
    self.selectedIndex = to;
}


-(void)setupAllTabBarController
{

    JOHomeViewController *home = [[JOHomeViewController alloc] init];
    home.tabBarItem.badgeValue= @"2000999";
    [self setupChildViewController:home title:@"首页" imageName:@"tabbar_home" selectedImageName:@"tabbar_home_selected"];
    
    
    
    
    JOMessageViewController *message = [[JOMessageViewController alloc] init];
    message.tabBarItem.badgeValue = @"233";
    [self setupChildViewController:message title:@"消息" imageName:@"tabbar_message_center" selectedImageName:@"tabbar_message_center_selected"];
    
    
//    JODiscoverController *discover = [[JODiscoverController alloc] init];
    
    JODiscoverController *discover = [[UIStoryboard storyboardWithName:@"Discover" bundle:nil] instantiateViewControllerWithIdentifier:@"Discover"];
    discover.tabBarItem.badgeValue = @"9999";
    [self setupChildViewController:discover title:@"广场" imageName:@"tabbar_discover" selectedImageName:@"tabbar_discover_selected"];
    
    JOMeController *me = [[JOMeController alloc] init];
    me.tabBarItem.badgeValue = @"8888";
    [self setupChildViewController:me title:@"我" imageName:@"tabbar_profile" selectedImageName:@"tabbar_profile_selected"];
}


-(void)setupChildViewController:(UIViewController *)childVC title:(NSString *)title imageName:(NSString *)imageName  selectedImageName:(NSString *)selectName
{
//    childVC.view.backgroundColor = [UIColor redColor];
    childVC.title =title;
    childVC.tabBarItem.title = title;
    childVC.tabBarItem.image =[UIImage imageNamed:imageName];
    childVC.tabBarItem.selectedImage = [[UIImage imageNamed:selectName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
    
    JONavigationController *Nav = [[JONavigationController alloc] initWithRootViewController:childVC];
    [self.customTabBar addTabBarButtonWithItem:childVC.tabBarItem];
    
    [self addChildViewController:Nav];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}




@end
